package com.syxp.pda.action.system;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.syxp.pda.core.bean.FieldBean;
import com.syxp.pda.core.command.QueryFilter;
import com.syxp.pda.core.web.action.BaseAction;
import com.syxp.pda.entity.system.FunctionDefine;
import com.syxp.pda.entity.system.Role;
import com.syxp.pda.service.system.FuncService;
import com.syxp.pda.service.system.RoleService;

public class RoleAction extends BaseAction {
	@Resource
	private RoleService roleService;
	@Resource
	private FuncService funcService;
	
	private Role role;
	private int roleID;
	private String funcParentID;
	private String funcIDs;
	
	private String funcCode;
	
	/**
	 * @return the roleService
	 */
	public RoleService getRoleService() {
		return roleService;
	}

	/**
	 * @param roleService the roleService to set
	 */
	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}

	/**
	 * @return the role
	 */
	public Role getRole() {
		return role;
	}

	/**
	 * @param role the role to set
	 */
	public void setRole(Role role) {
		this.role = role;
	}

	/**
	 * @return the roleID
	 */
	public int getRoleID() {
		return roleID;
	}

	/**
	 * @param roleID the roleID to set
	 */
	public void setRoleID(int roleID) {
		this.roleID = roleID;
	}

	/**
	 * @return the funcService
	 */
	public FuncService getFuncService() {
		return funcService;
	}

	/**
	 * @param funcService the funcService to set
	 */
	public void setFuncService(FuncService funcService) {
		this.funcService = funcService;
	}

	/**
	 * @return the funcParentID
	 */
	public String getFuncParentID() {
		return funcParentID;
	}

	/**
	 * @param funcParentID the funcParentID to set
	 */
	public void setFuncParentID(String funcParentID) {
		this.funcParentID = funcParentID;
	}

	/**
	 * @return the funcIDs
	 */
	public String getFuncIDs() {
		return funcIDs;
	}

	/**
	 * @param funcIDs the funcIDs to set
	 */
	public void setFuncIDs(String funcIDs) {
		this.funcIDs = funcIDs;
	}

	/**
	 * @return the funcCode
	 */
	public String getFuncCode() {
		return funcCode;
	}

	/**
	 * @param funcCode the funcCode to set
	 */
	public void setFuncCode(String funcCode) {
		this.funcCode = funcCode;
	}

	public String load() {
		List list = new ArrayList(); 
		try {
			List<Role> roles = this.roleService.getDao().getAll();
			for(Role o: roles) {
				Map map = new HashMap();
				map.put("id", o.getRoleID());
				map.put("code", o.getRoleCode());
				map.put("name", o.getRoleName());
				map.put("remarks", o.getRemarks());
				list.add(map);
			}
		} catch(Exception e) {
			e.printStackTrace();
		}
		this.jsonString = this.gson.toJson(list);
		System.out.println(this.jsonString);
		return SUCCESS;
	}
	
	public String save() {
		try {
			this.roleService.addService(role);
			this.jsonString="{success:true}" ;
		} catch(Exception e) {
			e.printStackTrace();
			this.jsonString="{success:false}" ;
		}
		return SUCCESS;
	}
	
	public String delete() {
		try {System.out.println("roleid="+roleID);
			this.roleService.removeService(this.roleID);
			this.jsonString="{success:true}" ;
		} catch(Exception e) {
			e.printStackTrace();
			this.jsonString="{success:false}" ;
		}
		return SUCCESS;
	}
	
	public String update() {
		try {
			this.roleService.update(this.role);
			this.jsonString="{success:true}" ;
		} catch(Exception e) {
			e.printStackTrace();
			this.jsonString="{success:false}" ;
		}
		return SUCCESS;
	}
	
	public String loadtree() {
		List list = new ArrayList(); 
		try {
			List<Role> roles = this.roleService.getDao().getAll();
			for(Role o: roles) {
				Map map = new HashMap();
				map.put("id", o.getRoleID());
				map.put("text", o.getRoleName());
				map.put("leaf", true);
				list.add(map);
			}
		} catch(Exception e) {
			e.printStackTrace();
		}
		this.jsonString = this.gson.toJson(list);
		System.out.println(this.jsonString);
		return SUCCESS;
	}
	
	public String loadPrivilege() {
		System.out.println("parid="+this.funcParentID+" roleid="+this.roleID);
		try {QueryFilter filter = new QueryFilter() ;
		String[] temp = funcParentID.split("_") ;
		filter.addFilter(new FieldBean(temp[0],Integer.valueOf(temp[1]),temp[2])) ;
		List<FunctionDefine> all = funcService.getDao().getAll(filter) ;
		JsonArray jarray = new JsonArray() ;
		for(FunctionDefine func : all) {
			JsonObject jobj = new JsonObject() ;
			jobj.addProperty("id", String.valueOf(func.getFunctionID())) ;
			jobj.addProperty("text", func.getFunctionName()) ;
			boolean b = false;
			if(func.getRoles()!=null&&func.getRoles().size()>0) {
				for(Role role: func.getRoles()) {
					if(this.roleID==role.getRoleID()) {
						b = true;
						break;
					}
				}
			}
			jobj.addProperty("checked", b);
			//jobj.addProperty("cls", "folder") ;
			jarray.add(jobj) ;
			this.jsonString = gson.toJson(jarray) ;
		}
		} catch (Exception e) {
			this.jsonString = "[]";
			e.printStackTrace();
		}
		System.out.println(this.jsonString);
		return SUCCESS;
	}
	
	public String setPrivilege() {
		System.out.println("FUNCIDS="+this.funcIDs+" roleid="+this.roleID);
		try {
			if(this.funcIDs!=null&&!"".equals(this.funcIDs)) {
				String[] funcIDstr = this.funcIDs.split(",");
				List<Integer> list = new ArrayList<Integer>();
				for(String ids: funcIDstr) {
					list.add(Integer.parseInt(ids));
				}
				this.roleService.setPrivilege(this.roleID, list);
			}
			this.jsonString="{success:true}" ;
		} catch (Exception e) {
			this.jsonString="{success:false}" ;
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	public String loadFunc() {
		try {
			int roleId = (Integer) session.get("roleid");
			JsonArray jarray = new JsonArray() ;
			if(this.funcCode==null) {
				List<FunctionDefine> all = this.roleService.loadPrivilege(roleId)[0];
				for(FunctionDefine func : all) {
					if(func.getIsModule()==1) {
						JsonObject jobj = new JsonObject() ;
						jobj.addProperty("id", String.valueOf(func.getFunctionID())) ;
						jobj.addProperty("functioncode", func.getFunctionCode()) ;
						jobj.addProperty("functionname", func.getFunctionName()) ;
						jarray.add(jobj) ;
					}
				}
			} else {
				QueryFilter filter = new QueryFilter() ;
				filter.addFilter(new FieldBean("functionCode",this.funcCode,"EQ")) ;
				List<FunctionDefine> l1 = this.funcService.getDao().getAll(filter) ;
				if(l1!=null&&l1.size()>0) {
					FunctionDefine fd = l1.get(0);
					QueryFilter filter1 = new QueryFilter() ;
					filter1.addFilter(new FieldBean("parentID",fd.getFunctionID(),"EQ")) ;
					List<FunctionDefine> l2 = this.funcService.getDao().getAll(filter1) ;
					for(FunctionDefine func : l2) {
						JsonObject jobj = new JsonObject() ;
						jobj.addProperty("id", String.valueOf(func.getFunctionID())) ;
						jobj.addProperty("functioncode", func.getFunctionCode()) ;
						jobj.addProperty("functionname", func.getFunctionName()) ;
						jarray.add(jobj) ;
					}
					System.out.println(fd.getFunctionName()+l2.size());
				}
			}
			this.jsonString = gson.toJson(jarray) ;
		} catch (Exception e) {
			this.jsonString = "[]";
			e.printStackTrace();
		}
		System.out.println(this.jsonString);
		return SUCCESS;
	}
	
}
